import 'package:card_topia/common_widget/my_app_bar.dart';
import 'package:card_topia/common_widget/my_scaffold.dart';
import 'package:card_topia/design_specifications.dart';
import 'package:card_topia/my_router.dart';
import 'package:card_topia/pages/login_page/verify_code_page.dart';
import 'package:card_topia/service/common_service.dart';
import 'package:card_topia/utils/dialog_util.dart';
import 'package:ficonsax/ficonsax.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../common_widget/loading_button.dart';
import 'login_pwd_page.dart';

///登录页面

class LoginPage extends ConsumerStatefulWidget {
  const LoginPage({super.key});

  @override
  ConsumerState<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends ConsumerState<LoginPage> {
  final TextEditingController _controller = TextEditingController();
  final FocusNode _focusNode = FocusNode();

  bool _enable = false;
  bool _agree = false;
  @override
  void initState() {
    super.initState();
    _controller.addListener(() {
      listen();
    });
  }

  //监听输入框
  listen() {
      if (_controller.text.isNotEmpty) {
        _enable = true;
      } else {
        _enable = false;
      }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        isBack: false,
        showLeading: true,
        actions: [
          InkWell(
            onTap: () {
              FocusScope.of(context).requestFocus(FocusNode());
              // pushPage(context, page: const LoginPwdPage());
              MyRouter.push(page: LoginPwdPage());
            },
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 16.w),
              child: Text(
                "密码登录",
                style: TextStyle(fontSize: 14.sp, fontFamily: 'Harmony'),
              ),
            ),
          )
        ],
      ),
      body: Column(
        children: [
          Container(
            margin: EdgeInsets.only(left: 16.w, top: 24.w),
            alignment: Alignment.centerLeft,
            child: Text(
              "登录",
              style: TextStyle(fontFamily: 'Harmony', fontSize: 24.sp),
            ),
          ),
          //邮箱输入
          SizedBox(
            height: 24.w,
          ),
          Container(
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(8)),
            margin: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.w),
            padding: EdgeInsets.symmetric(
              horizontal: 12.w,
            ),
            height: 48.w,
            child: Row(
              children: [
                Icon(IconsaxOutline.sms,color: _controller.text.isNotEmpty?textColor:disableColor,),
                SizedBox(
                  width: 12.w,
                ),
                Expanded(
                  child: TextField(
                    focusNode: _focusNode,
                    controller: _controller,
                    autofocus: true,
                    cursorColor: themeColor,
                    keyboardType: TextInputType.emailAddress,
                    style: TextStyle(
                        fontFamily: harmony,
                        fontSize: 16.sp,
                        color: textColor),
                    inputFormatters: [
                      FilteringTextInputFormatter.deny(RegExp(r'\s'))
                    ],
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        isCollapsed: true,
                        hintText: "请输入邮箱",
                        hintStyle: TextStyle(
                            fontFamily: harmony,
                            fontSize: 16.sp,
                            color: disableColor)),
                  ),
                )
              ],
            ),
          ),
          SizedBox(height: 16.w,),
          LoadingButton(
            enable: _enable,
            width: MediaQuery.of(context).size.width-32.w,
            text: "获取验证码",
            loadingText: "发送中...",
            onTap: ()async{
              FocusScope.of(context).requestFocus(FocusNode());
              if(!_agree){
                DialogUtil.showToast(
                  text: '请先阅读并同意用户协议和隐私政策'
                );
                return;
              }
              final result = await CommonService.sendEmailCode(email: _controller.text);
              if(result) {
                  if(context.mounted){
                    // context.push(MyRouter.verifyCodePage,extra: {"email":_controller.text});
                    MyRouter.push(page: VerifyCodePage(email: _controller.text));
                  }
                }
              }
          ),
          SizedBox(height: 16.w,),
          InkWell(
            onTap: ()=>setState(() {
              _agree = !_agree;
            }),
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 16.w),
              child: Row(
                children: [
                  Icon(_agree?Icons.radio_button_checked_rounded:Icons.circle_outlined,size: 18.w,color: _agree?themeColor:disableColor,),
                  SizedBox(width: 4.w,),
                  Text("已阅读并同意",style: TextStyle(fontFamily: harmony,fontSize: 12.sp),),
                  Text("用户协议",style: TextStyle(fontFamily: harmony,fontSize: 12.sp,color: Colors.blue),),
                  Text("和",style: TextStyle(fontFamily: harmony,fontSize: 12.sp),),
                  Text("隐私政策",style: TextStyle(fontFamily: harmony,fontSize: 12.sp,color: Colors.blue),),
                ],
              ),
            ),
          )

        ],
      ),
    ).withOnTap(context);
  }
}
